package LinkedList;

public class _725_SplitLinkedListinParts {
    public ListNode[] splitListToParts(ListNode root, int k) {
        ListNode cur = root;
        int N = 0;
        while(cur!=null){
            cur = cur.next;
            N++;
        }
        int part = N/k;
        int remains = N%k;
        cur = root;
        ListNode[] nodeLists = new ListNode[k];
        for(int i=0;i<k;i++){
            ListNode head = new ListNode(0);
            ListNode temp = head;
            for(int j=0;j<(part+(i<remains?1:0));j++){
                temp.next = cur;
                temp = temp.next;
                if(cur!=null){
                    cur = cur.next;
                }
            }
            if(temp!=null){
                temp.next = null;
            }
            nodeLists[i] = head.next;
        }
        return nodeLists;
    }
}
